package com.spring.rest;

import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import com.spring.rest.vo.Request;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.ResponseEntity;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;

/**
 * httpClient 最佳实践
 * @author chenhaiyang
 */
public class RestDemoUtil {

    private static RestTemplate restTemplate = new RestTemplate();

    public static void main(String[] args) {

        Request request = new Request();
        request.setAge("12");
        request.setUserId("22");

        jasrx(request);
        templateClient(request);

    }

    private static void jasrx(Request request) {
        // jax-rs 调用rest 请求协议
        ResteasyProviderFactory factory = new ResteasyProviderFactory();

        JacksonJsonProvider jacksonJsonProvider = new JacksonJsonProvider();
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        jacksonJsonProvider.setMapper(objectMapper);
        factory.register(jacksonJsonProvider);

        Client client = ClientBuilder.newClient(factory);


        WebTarget target = client.target("http://127.0.0.1:9085/AppCenter/getConfigs");
        Response response = target.request().post(Entity.entity(request, MediaType.APPLICATION_JSON_TYPE));
        try {
            if(response.getStatus() != HttpStatus.OK.value()) {
                System.out.println(response.getStatus());
            }
            List<String> result = response.readEntity(new GenericType<List<String>>(){});
            System.out.println(result);
        } finally {
            response.close();
        }
    }

    /**
     * restTemplate请求
     * 不支持gzip压缩，如果想restTemplate支持压缩，
     * @param request 请求
     */
    private static void templateClient( Request request) {
        List<HttpMessageConverter<?>> results =  restTemplate.getMessageConverters();
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = (MappingJackson2HttpMessageConverter) results.get(6);
        //这句话实现了json忽略不必要的字段
        mappingJackson2HttpMessageConverter.getObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        ResponseEntity<List<String>> responseEntity = restTemplate.exchange("http://127.0.0.1:9085/AppCenter/getConfigs",org.springframework.http.HttpMethod.POST, new HttpEntity<>(request),new ParameterizedTypeReference<List<String>>(){});
        System.out.println(responseEntity.getBody());
    }
}
